<template>
  <el-dialog
    v-model="dialogVisible"
    title="编辑员工信息"
    width="600px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form :model="employeeForm" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="用户名">
        <el-input v-model="employeeForm.username" disabled />
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-input v-model="employeeForm.realName" disabled />
      </el-form-item>
      <el-form-item label="职位" prop="position">
        <el-input v-model="employeeForm.position" placeholder="请输入职位" />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input v-model="employeeForm.department" placeholder="请输入部门" />
      </el-form-item>
      <el-form-item label="员工编号" prop="employeeNo">
        <el-input v-model="employeeForm.employeeNo" placeholder="请输入员工编号" />
      </el-form-item>
      <el-form-item label="入职日期" prop="joinDate">
        <el-date-picker
          v-model="employeeForm.joinDate"
          type="date"
          placeholder="选择入职日期"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="employeeForm.status" placeholder="请选择状态" style="width: 100%">
          <el-option label="在职" :value="1" />
          <el-option label="离职" :value="0" />
          <el-option label="待入职" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="employeeForm.remark"
          type="textarea"
          placeholder="请输入备注信息"
          :rows="3"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleConfirmEdit" :loading="submitting">
        确认修改
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { propertyEmployeeApi } from '@/api/property/employee'
import type { EmployeeVO } from '@/api/property/employee/types'
import { showSuccess, showError } from '@/utils/message'

// Props
interface Props {
  visible: boolean
  propertyCompanyId: number
  employee: EmployeeVO | null
}

const props = defineProps<Props>()

// Emits
const emit = defineEmits<{
  'update:visible': [value: boolean]
  'success': []
}>()

// 响应式数据
const dialogVisible = ref(false)
const submitting = ref(false)

const employeeForm = ref({
  id: 0,
  username: '',
  realName: '',
  position: '',
  department: '',
  employeeNo: '',
  joinDate: null as Date | null,
  status: 1,
  remark: ''
})

const formRef = ref<FormInstance>()

// 表单验证规则
const rules: FormRules = {
  position: [
    { required: true, message: '请输入职位', trigger: 'blur' }
  ],
  department: [
    { required: true, message: '请输入部门', trigger: 'blur' }
  ],
  employeeNo: [
    { required: true, message: '请输入员工编号', trigger: 'blur' }
  ],
  joinDate: [
    { required: true, message: '请选择入职日期', trigger: 'change' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 监听visible变化
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal
  if (newVal && props.employee) {
    initForm()
  }
})

watch(dialogVisible, (newVal) => {
  emit('update:visible', newVal)
})

// 方法
const initForm = () => {
  if (!props.employee) return

  employeeForm.value = {
    id: props.employee.id,
    username: props.employee.username || '',
    realName: props.employee.realName || '',
    position: props.employee.position || '',
    department: props.employee.department || '',
    employeeNo: props.employee.employeeNo || '',
    joinDate: props.employee.joinDate ? new Date(props.employee.joinDate) : null,
    status: props.employee.status || 1,
    remark: props.employee.remark || ''
  }
}

const handleClose = () => {
  dialogVisible.value = false
}

const handleConfirmEdit = async () => {
  try {
    await formRef.value?.validate()
  } catch (error) {
    return
  }

  try {
    submitting.value = true
    
    const updateEmployeeDTO = {
      position: employeeForm.value.position,
      department: employeeForm.value.department,
      employeeNo: employeeForm.value.employeeNo,
      joinDate: employeeForm.value.joinDate?.toISOString().split('T')[0] || '',
      status: employeeForm.value.status,
      remark: employeeForm.value.remark
    }

    await propertyEmployeeApi.updateEmployeeInfo(
      props.propertyCompanyId,
      employeeForm.value.id,
      updateEmployeeDTO
    )

    showSuccess('更新员工信息成功')
    emit('success')
  } catch (error) {
    showError('更新员工信息失败')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.el-form {
  padding: 20px 0;
}
</style> 